package com.doit.demo02;

import java.util.concurrent.atomic.AtomicInteger;
@SuppressWarnings("all")
public class test_AtomicInteger {
    static int num = 0;
    static int numm = 0;
    static AtomicInteger ai = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
    Object obj = new Object();

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    num++;
                    ai.incrementAndGet();
                    synchronized (obj)
                    {
                        numm++;
                    }
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    num++;
                    ai.incrementAndGet();
                    synchronized (obj)
                    {
                        numm++;
                    }
                }
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println(num);//普通
        System.out.println(ai);//互斥锁 lock
        System.out.println(numm);//原子类
    }
}